package com.mdinic.blogovi.front.common;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.internal.HtmlHeaderContainer;
import org.apache.wicket.util.cookies.CookieUtils;

import com.mdinic.blogovi.front.common.login.LoginWindow;
import com.mdinic.blogovi.front.common.menu.LinkMenu;
import com.mdinic.blogovi.front.common.menu.TopMenu;
import com.mdinic.blogovi.front.common.menu.TopMenuPerson;
import com.mdinic.blogovi.front.page.BSession;
import com.mdinic.blogovi.front.page.HomePage;
import com.mdinic.blogovi.model.Person;

/**
 * abstract page that should be extended by all pages.
 * 
 * @author mdinic
 * 
 */
public abstract class AbstractBasePage extends WebPage {

    private static final String AUTH_CODE_COOKIE = "bauth";

    private static final long serialVersionUID = -4083976610900664777L;

    public static final String URL_PARAM_NAME = "id";

    public static final String PANEL = "panel";

    private final ModalWindow modalWindow;

    /**
     * Constructor that is invoked when page is invoked without a session.
     * 
     * @param parameters
     *            Page parameters
     */
    protected AbstractBasePage(boolean needUser) {
        super();

        if (!BSession.get().isLoggedIn()) {
            // try cookie
            CookieUtils cookieUtils = new CookieUtils();
            String code = cookieUtils.load(AUTH_CODE_COOKIE);
            if (StringUtils.isNotBlank(code)) {
                List<Person> result = Person.findPeopleByAuthEquals(code);
                if (!result.isEmpty()) {
                    BSession.get().setPerson(result.get(0));
                }
            }
        }

        if (needUser && !BSession.get().isLoggedIn()) {
            throw new RestartResponseException(HomePage.class);
        }

        topMenu();

        add(new LinkMenu());

        modalWindow = new ModalWindow("login_window") {

            private static final long serialVersionUID = 1L;

            @Override
            public void renderHead(HtmlHeaderContainer container) {
                super.renderHead(container);
                IHeaderResponse response = container.getHeaderResponse();
                response.render(OnDomReadyHeaderItem.forScript("Wicket.Window.unloadConfirmation = false;"));
            }
        };
        modalWindow.setTitle("Prijava");
        modalWindow.setResizable(false);

        add(modalWindow);

        add(new KeepAliveBehavior());
    }

    public void topMenu() {
        if (BSession.get().isLoggedIn()) {
            addOrReplace(new TopMenuPerson());
        } else {
            addOrReplace(new TopMenu());
        }
    }

    public void showLogin(AjaxRequestTarget target, IReturn iReturn) {
        modalWindow.setContent(new LoginWindow(target, modalWindow.getContentId(), iReturn));
        modalWindow.setAutoSize(true);
        modalWindow.show(target);
    }

    public ModalWindow getLoginModalWindow() {
        return modalWindow;
    }
}
